import { CHINESE, CHAR } from '@/config/regex'
// 获取字典
export const maps = function (key) {
  let arr = []
  if (this.pageMap && this.pageMap[key]) {
    arr = this.pageMap[key]
  } else if (this.map && this.map[key]) {
    arr = this.map[key]
  }
  return arr
}

// 格式化时间 type =3 时间戳转时间 yyyy-mm-dd hh:mm:ss
export const formatDate = (strTime, type = 0) => {
  if (!strTime) {
    return '无'
  }
  if (type === 0) {
    let date = new Date(strTime)
    let year = date.getFullYear()
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minute + ':' + sec
  } else if (type === 1) {
    let date = new Date(strTime)
    let year = date.getFullYear()
    let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    return year + '-' + month + '-' + day
  } else if (type === 2) {
    let date = new Date(strTime)
    let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    return hours + ':' + minute
  } else if (type === 3) {
    let date = new Date()
    let year = date.getFullYear()
    var month = date.getMonth() + 1 < 10 ? ('0' + date.getMonth() + 1) : date.getMonth() + 1
    let day = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
    let hours = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
    let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minute + ':' + second
  }
}

// 金额千分位
export const formatMoney = (money, type = 0) => {
  // let unit = '元'
  // if (money > 10000) {
  //   money = money / 10000
  //   unit = '万'
  // }
  let arr = (money + '').split('.')
  money = +arr[0]
  let reg = /\d{1,3}(?=(\d{3})+$)/g
  money = (money + '').replace(reg, '$&,')
  if (arr.length > 1) {
    money += '.' + arr[1]
  }
  return money
}

// 金额带两位小数
export const floatPrice = (price) => {
  let f = parseFloat(price)
  if (isNaN(f)) return false
  f = Math.round(price * 100) / 100
  let _result = f.toString()
  let rs = _result.indexOf('.')
  if (rs < 0) {
    rs = _result.length
    _result += '.'
  }
  while (_result.length <= rs + 2) {
    _result += '0'
  }
  return _result
}

// 格式化倒计时
export const getLastTime = function (timeData) {
  let time = parseInt(timeData / 1000)
  let day = parseInt(time / (3600 * 24))
  time = time % (3600 * 24)
  let hours = parseInt(time / (3600))
  time = time % 3600
  let minute = parseInt(time / (60))
  // time = time % (60)
  // let sec = time
  return day + ' 天 ' + hours + ' 小时 ' + minute + ' 分 '
}

// 手机号码中间4位虚化
export const phoneReplace = (phone = '') => {
  let hiddenNumber = phone.substring(3, 7)
  phone = phone.replace(hiddenNumber, '****')
  return phone
}
// 放大镜
export const $ = (select, type) => {
  if (typeof select !== 'string') return console.error('select 是个字符串')
  if (!type) {
    return document.querySelector(select)
  } else {
    return document.querySelectorAll(select)
  }
}
// 元素的offset top,left 值
export const offset = (el) => {
  let top = el.offsetTop
  let left = el.offsetLeft
  while (el.offsetParent) {
    el = el.offsetParent
    top += el.offsetTop
    left += el.offsetLeft
  }
  return {
    left: left,
    top: top
  }
}
// 数组扁平化
export const flattenDepth = (array, depth = 1) => {
  let output = []
  array.forEach(element => {
    let d = depth
    if (Array.isArray(element) && d > 0) {
      output.push(...flattenDepth(element, --d))
    } else {
      output.push(element)
    }
  })
  return output
}
// route的query,形成键值对
export const getQuery = (query) => {
  let arr = []
  let obj = {}
  arr = query.split('&')
  arr.map((item, index) => {
    let ind = item.indexOf('=')
    let key = item.substring(0, ind)
    obj[key] = item.substring(ind + 1)
  })
  return obj
}
// 生成随机数
export const randomDigit = () => {
  let date = Date.parse(new Date())
  let random = Math.floor(Math.random() * 900000 + 100000)
  return date + random
}
/**
 * 函数节流 频率调控 返回函数连续调用时,func的执行频率是 次/wait
 * @param func {Function} 实际要执行的函数
 * @param wait {Number} 执行时间间隔 单位是毫秒 默认是1000毫秒
 * @return {Function} 返回一个节流函数
 */
export const throttle = (func, wait = 1000) => {
  let timer = null
  // 前一次执行函数的时间
  let previous
  return function () {
    const context = this
    const args = arguments
    const now = +new Date()
    if (previous && now < previous + wait) {
      clearTimeout(timer)
      timer = setTimeout(function () {
        func.apply(context, args)
        previous = now
      }, wait)
    } else {
      func.apply(context, args)
      previous = now
    }
  }
}
/**
 * 一维数组去重
 * @param arr 实际要去重的数组
 * @return n 返回去掉重复值的数组
 */
export const unique = (arr) => {
  let n = [arr[0]]
  for (let i = 1; i < arr.length; i++) {
    if (arr.indexOf(arr[i]) === i) n.push(arr[i])
  }
  return n
}
/**
 * 倒计时countDown
 */
export const countDown = (ptimeStar = '') => {
  if (!ptimeStar) { throw new Error('时间参数不能为空') }
  let timeStarTime
  const timeStar = new Date(ptimeStar.replace(/-/g, '/'))
  timeStarTime = timeStar.getTime()
  let nowTime = (new Date()).getTime()
  // 剩余时间
  let leftStarTime = timeStarTime - nowTime
  if (leftStarTime <= 0) return false

  const zero = (value) => {
    if (value < 10) {
      return '0' + value
    }
    return value
  }

  let d = Math.floor(leftStarTime / 24 / 60 / 60 / 1000)
  let h = Math.floor(leftStarTime / 1000 / 60 / 60 % 24)
  let m = Math.floor(leftStarTime / 1000 / 60 % 60)
  let s = Math.floor(leftStarTime / 1000 % 60)

  return `${d}:${zero(h)}:${zero(m)}:${zero(s)}`
}
/**
 * 两个时间是否为同一周
 */
export const isSameWeek = (time1, time2) => {
  if (!time1 || !time2) { throw new Error('时间参数不能为空') }
  const time1Stamp = new Date(time1.replace(/-/g, '/')).getTime()
  const time2Stamp = new Date(time2.replace(/-/g, '/')).getTime()
  console.log(time1Stamp, time2Stamp)
  const oneDayTime = 1000 * 60 * 60 * 24
  let oldcount = parseInt(time1Stamp / oneDayTime)
  let nowcount = parseInt(time2Stamp / oneDayTime)
  return parseInt((oldcount + 4) / 7) === parseInt((nowcount + 4) / 7)
}

// 是否是中文汉字
export function isChinese (str) {
  const rlt = CHINESE.test(str)
  return rlt
}

// 是否是字符串

export function isChar (str) {
  const rlt = CHAR.test(str)
  return rlt
}
